<?php
namespace adapter;

class MySQLi implements DatabaseInterface
{
    protected $conn;

    public function connect($host, $port, $dbName, $user, $pwd)
    {
        $conn = mysqli_connect($host, $user, $pwd, $dbName, $port);        
        if(mysqli_connect_errno($conn)) {
            echo '数据库连接失败: ' . mysqli_connect_error();
            return false;
        }
        $this->conn = $conn;
        mysqli_query($this->conn, 'set names utf8'); //设置字符编码
    }

    public function query($sql)
    {
        $res = mysqli_query($this->conn, $sql);
        $list = [];
        if($res) {
            while($row = mysqli_fetch_assoc($res)){
                $list[] = $row;
            }
        }        
        mysqli_free_result($res);
        return $list;
    }

    public function close()
    {
        mysqli_close($this->conn);
    }

}



